*********************************************************
** Go through marketscan red books and find ndc numbers
** that are for oxycodone
*********************************************************

clear
set more off

foreach x in "2003" "2004" "2005" "2006" "2007" "2008_2009" "2010_2013" {

	use /disk/aging/mktscan/docs/redbook/`x'/redbook.dta, clear
	gen oxycodone = strpos(prodnme,"OXYCODONE")>0 | strpos(upper(gennme),"OXYCODONE")>0
	gen oxycontin = strpos(prodnme,"OXYCONTIN")>0 | strpos(upper(gennme),"OXYCONTIN")>0
	keep if oxycodone==1 | oxycontin==1

	** now get the milligrams per pill information
	replace strngth=trim(lower(strngth))
	gen mgs = .
	** for pills
	replace mgs = 2.25 if strngth=="2.25 mg-325 mg" | strngth=="325 mg-2.25 mg-0.19 mg" 
	replace mgs = 2.5 if strngth=="325 mg-2.5 mg" | strngth=="300 mg-2.5 mg" | strngth=="400 mg-2.5 mg" 
	replace mgs = 4.5 if strngth=="325 mg-4.5 mg-0.38 mg" | strngth=="4.5 mg-325 mg" 
	replace mgs = 4.8355 if strngth=="325 mg-4.8355 mg"
	replace mgs = 5 if strngth=="5 mg" | strngth=="325 mg-5 mg" | strngth=="5 mg-325 mg" | strngth=="5 mg-500 mg" | strngth=="500 mg-5 mg" | strngth=="400 mg-5 mg" | strngth=="300 mg-5 mg" 
	replace mgs = 7.5 if strngth=="325 mg-7.5 mg" | strngth=="500 mg-7.5 mg" | strngth=="300 mg-7.5 mg" | strngth=="400 mg-7.5 mg" | strngth=="7.5 mg"
	replace mgs = 10 if strngth=="10 mg" | strngth=="325 mg-10 mg" | strngth=="650 mg-10 mg"| strngth=="500 mg-10 mg" | strngth=="300 mg-10 mg" | strngth=="400 mg-10 mg"
	replace mgs = 15 if strngth=="15 mg"
	replace mgs = 20 if strngth=="20 mg"
	replace mgs = 30 if strngth=="30 mg"
	replace mgs = 40 if strngth=="40 mg"
	replace mgs = 60 if strngth=="60 mg"
	replace mgs = 80 if strngth=="80 mg" 
	replace mgs = 160 if strngth=="160 mg"
	** for solutions
	gen mgs_sol = .
	replace mgs_sol = 20 if strngth=="20 mg/ml"
	replace mgs_sol = 5 if strngth=="325 mg/5 ml-5 mg/5 ml" | strngth=="5 mg/5 ml"
	** get number of milliliters so you can see how much oxy is in a solution Rx
	gen mls_sol = substr(metsize,1,strpos(metsize,".")-1) if strpos(metsize, "MLX")==0 & strpos(metsize, "ML")>0
	replace mls_sol = substr(metsize,strpos(metsize,"MLX")+3,.) if strpos(metsize, "MLX")>0
	destring mls_sol, replace 

	** now create mgs per Rx
	gen mgs_rx = pksize*mgs
	replace mgs_rx = mls_sol*mgs_sol if mgs_rx==.
	tab strngth mgs, miss

	keep ndcnum oxycodone oxycontin mgs_rx
	compress
	sort ndcnum
	if "`x'" != "2008_2009" & "`x'" != "2010_2013" {
		save ./ndcnums_`x'.dta, replace
	}
	else if "`x'" == "2008_2009" {
		save ./ndcnums_2008.dta, replace
		save ./ndcnums_2009.dta, replace
	}
	else {
		save ./ndcnums_2010.dta, replace
		save ./ndcnums_2011.dta, replace
		save ./ndcnums_2012.dta, replace
		save ./ndcnums_2013.dta, replace
	}
}

